package com.lavender.annotation._async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Service
public class TestService {

    /**
     * @Async 修饰无返回值
     */
    @Async
    public void hello1() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("time is " + LocalDateTime.now().toString() + ", hello1");
    }

    /**
     * @Async 修饰有返回值
     */
    @Async
    public Future<String> hello2() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("time is " + LocalDateTime.now().toString() + ", hello2");
        return new AsyncResult<>("ok");
    }

}
